<?php

class Acl_Users_Users extends Acl_Users_UsersAbstract
{
		
	public function getUsers()
	{		
		$sql="SELECT * FROM users";
		return $this->db->fetchArray($sql);		
	}
	
	public function getUser($id)
	{
		$sql="SELECT * FROM users WHERE id=".$id;
		return $this->db->fetchOneArray($sql);
			
	}
	
	public function insertUser($data, $file)
	{
		$lastId=0;
		if($file['image']['name']!='')
		{
			$filename=Acl_Images_Image::uploadImage();
			$image="image='".$filename."', ";
		}
		else 
			$image='';	
		
		$sql="INSERT INTO users SET
				name='".$data['name']."',
				email='".$data['email']."',
				phone='".$data['phone']."',
				address='".$data['address']."',
				postalcode='".$data['postalcode']."',
				city='".$data['city']."',
				password='".sha1($data['password'])."',
				gender='".$data['gender']."',
				".$image."
				provinces_id='".$data['province']."',
				description='".$data['description']."',
				firm='".md5($data['name'].$data['email'].$data['phone'].
						$data['address'].$data['postalcode'].$data['city'].
						sha1($data['password']).$data['gender'].$data['description'].
						$this->privateKey)."'
				";
			
		
		$this->db->execute($sql);
		// TODO: rewrite into class Mysql_Connect
		$lastId=mysql_insert_id();
	
		//Insertar en languages
		foreach($data['languages'] as $value)
		{
			$sql="INSERT INTO users_has_languages SET
							users_id='".$lastId."',
							languages_id='".$value."'
				";	
			$this->db->execute($sql);		
		}		
		//Insertar en likes
		foreach($data['likes'] as $value)
		{
			$sql="INSERT INTO users_has_likes SET
							users_id='".$lastId."',
							likes_id='".$value."'
				";	
			$this->db->execute($sql);	
		}		
		if($lastId==0)
			return FALSE;
		else
			return $lastId;
		
	}
	
	public function updateUser($data, $file)
	{
		if($data['firm']!=Acl_Firms_Firm::getFirm($data['id'],$this->privateKey))
			die("Muerte subita");
			
		$user=$this->getUser($data['id']);	
		
		$sql="UPDATE users SET
				name='".$data['name']."',
				email='".$data['email']."',
				phone='".$data['phone']."',
				address='".$data['address']."',
				postalcode='".$data['postalcode']."',
				city='".$data['city']."',
				gender='".$data['gender']."',
				provinces_id='".$data['province']."',
				description='".$data['description']."',
				firm='".md5($data['name'].$data['email'].$data['phone'].
						$data['address'].$data['postalcode'].$data['city'].
						$user['password'].$data['gender'].$data['description'].
						$this->privateKey)."' 			
				WHERE id=".$data['id'];
		
		$this->db->execute($sql);
		
	
		//Insertar en languages
		$sql="DELETE FROM users_has_languages WHERE users_id=".$data['id'];
		$this->db->execute($sql);
		foreach($data['languages'] as $value)
		{
			$sql="INSERT INTO users_has_languages SET
							users_id='".$data['id']."',
							languages_id='".$value."'
				";	
			$this->db->execute($sql);	
		}
		
		
				
		//Insertar en likes
		$sql="DELETE FROM users_has_likes WHERE users_id=".$data['id'];
		$this->db->execute($sql);
		foreach($data['likes'] as $value)
		{
			$sql="INSERT INTO users_has_likes SET
							users_id='".$data['id']."',
							likes_id='".$value."'
				";	
			$this->db->execute($sql);	
		}
		
		$user=$this->getUser($data['id']);
		if($file['image']['name']!='')
		{
			$file=$user['image'];
			unlink($_SERVER['DOCUMENT_ROOT'].'/assets/images/uploads/'.$file);
			$filename=Acl_Images_Image::uploadImage();	
			$sql="UPDATE users SET image='".$filename."' WHERE id=".$data['id'];
			$this->db->execute($sql);
			
		}
		
		return $data['id'];
		
	}
	
	public function deleteUser($data)
	{	
		
		$user=$this->getUser($data['id']);
		
		$sql="DELETE FROM users_has_likes WHERE users_id='".$data['id']."'";
		$this->db->execute($sql);
		$sql="DELETE FROM users_has_languages WHERE users_id='".$data['id']."'";
		$this->db->execute($sql);
		$file=$user['image'];
		unlink($_SERVER['DOCUMENT_ROOT'].'/assets/images/uploads/'.$file);
		$sql="DELETE FROM users WHERE id='".$data['id']."'";
		$this->db->execute($sql);
	
		return;
	}
	
	
	
}







